const routesConfig = ($stateProvider, $urlRouterProvider, $locationProvider) => {
  'ngInject';

  $locationProvider.html5Mode(true);
  $urlRouterProvider.otherwise('/login');

  $stateProvider
    .state('login', {
      url: '/login',
      component: 'loginPage'
    })
    .state('app', {
      abstract: true,
      component: 'appLayout',
      resolve: {
        auth: (AuthService, $state) => {
          'ngInject';
          if (!AuthService.isLoggedIn()) {
            $state.go('login');
            return false;
          }
          return true;
        }
      }
    })
    .state('app.home', {
      url: '/',
      component: 'homePage'
    })
    .state('app.todos', {
      url: '/todos',
      component: 'todosPage'
    })
    .state('app.ticketCreate', {
      url: '/ticket/create',
      component: 'ticketCreatePage'
    })
    .state('app.ticketQuery', {
      url: '/ticket/query',
      component: 'ticketQueryPage'
    })
    .state('app.ticketDetail', {
      url: '/ticket/:id',
      component: 'ticketDetailPage'
    })
    .state('app.settings', {
      url: '/settings',
      component: 'settingsPage'
    })
    .state('app.userManagement', {
      url: '/user-management',
      component: 'userManagementPage'
    });
};

// 等待 Angular 加载完成后再定义模块
function initRoutes() {
  const angular = window.angular;
  
  if (!angular) {
    console.error('Angular 未找到，无法初始化路由模块');
    return;
  }

  angular.module('app.routes', [])
    .config(routesConfig);
}

// 监听第三方库加载完成事件
window.addEventListener('vendorLibsLoaded', () => {
  initRoutes();
});

// 如果 Angular 已经加载完成，直接初始化
if (window.angular) {
  initRoutes();
}